//
//  WBStatusViewModel.swift
//  WeiBoApp
//
//  Created by James on 2017/7/17.
//  Copyright © 2017年 James. All rights reserved.
//

import Foundation

class WBStatusViewModel :CustomStringConvertible{
    //关于表格的性能优化
    /**
     - 尽量少计算，所有需要的素材提前计算好
     - 控件上不要设置圆角半径，所有图像渲染的属性，都要注意!
     - 不要动态创建控件，所有需要的控件，都要提前创建好，在显示的时候，根据数据  隐藏或显示
     - cell中控件的层次越少越好，数量越少越好
     */
    
    
    ///微博模型
    var status:WBStatus
    
    ///会员图标
    var memberIcon:UIImage?
    
    
    /// 认证图标
    var vipIcon:UIImage?
    
    /// 配图试图的大小
    var pictureViewSize = CGSize()
    
    
    init(model:WBStatus) {
        self.status = model
        if (model.user?.mbrank)! > 0 && (model.user?.mbrank)! < 7 {
            //会员等级
            let imgName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            self.memberIcon = UIImage(named: imgName)
        }
        
        switch model.user?.verified_type ?? -1 {
            case 0:
                vipIcon = UIImage(named: "avatar_vip")
            case 2,3,5:
                vipIcon = UIImage(named: "avatar_enterprise_vip")
            case 220:
                vipIcon = UIImage(named: "avatar_grassroot")
            default:
                ()
        }
        
        
        //计算配图试图大小
        pictureViewSize = calcPictureViewSize(count: status.pic_urls?.count ?? 0)
    }
    
    fileprivate func calcPictureViewSize(count:Int?) -> CGSize {
        
        if count == 0  {
            return CGSize()
        }
        
        //1.计算配图试图宽度
        //2.计算高度
        //1.根据count 知道行数  1~9
        let row = CGFloat( (count! - 1) / 3 + 1 )
        //根据行数计算高度
        let height = WBStatusPictureOutterMargin + row*WBStatusPictureItemWidth + (row-1)*WBStatusPictureViewInnerMargin
        
       return CGSize(width: WBStatusPictureWidth, height: height)
    }
    
    var description: String{
        return status.description
    }
}
